package o2o.web.actions.sys;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.sys.SysConfigSmsCache;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.sys.SysConfigSMS;
import o2o.dao.PO.user.UserInfo;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//短信配置管理
@Controller
public class SysConfigSMSAction {
    //获取所有短信配置信息
    @RequestMapping(method = RequestMethod.POST,value = "/admin/sys/config/sms/list")
    @ResponseBody
    public  String getAllSysConfigSMS(String token){
        //登录验证
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        //权限验证
        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有访问权限");
        }
        List<SysConfigSMS> list = SysConfigSmsCache.getInstance().getAllSysConfigSMS();
        return AjaxResult.successResult(list.size(),list);
    }


    //修改单个短信配置信息
    @RequestMapping(method = RequestMethod.POST,value = "/admin/sys/config/sms/update")
    @ResponseBody
    public String updateSysConfigSMS( String token, String smsType, String val, String sysAppKey, String note
            ,String extra1, String extra2, String extra3){
        //登录验证
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        //权限验证
        if (!User.getInstance().hasSuperAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有访问权限");
        }
        if(!ValidateUtil.isNotNull(smsType)){
            return AjaxResult.errorResult("标识不能为空");
        }

        SysConfigSMS getByType = SysConfigSmsCache.getInstance().getByType(smsType);
        if(null == getByType){
            return AjaxResult.errorResult("该短信配置不存在");
        }
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        boolean isUpdataSMS = false;
        StringBuffer sb  = new StringBuffer();
        sb.append("update ").append(PojoTables.SYS_CONFIG_SMS).append(" ").append(" set ");

        if(ValidateUtil.isNotNull(val) && !val.equals(getByType.getVal())){
            isUpdataSMS = true ;
            sb.append(" val = '").append(val).append("', ");
        }

        if(ValidateUtil.isNotNull(note) && !note.equals(getByType.getNote())){
            isUpdataSMS = true;
            sb.append(" note = '").append(note).append("', ");
        }

        if(ValidateUtil.isNotNull(sysAppKey) && !sysAppKey.equals(getByType.getSysAppKey())){
            isUpdataSMS = true;
            sb.append(" sysAppKey = '").append(sysAppKey).append("', ");
        }

        if(ValidateUtil.isNotNull(extra1) && !extra1.equals(getByType.getExtra1())){
            isUpdataSMS = true;
            sb.append( "extra1 = '").append(extra1).append("',");
        }

        if(ValidateUtil.isNotNull(extra2) && !extra2.equals(getByType.getExtra2())){
            isUpdataSMS = true;
            sb.append( "extra2 = '").append(extra2).append("',");
        }

        if(ValidateUtil.isNotNull(extra3) && !extra3.equals(getByType.getExtra3())){
            isUpdataSMS = true;
            sb.append( "extra3 = '").append(extra3).append("',");
        }

        sb.append(" updateUser = '").append(user.getUsername()).append("', updateTime = '").append(simpleDateFormat.format(date)).append("' ");
        sb.append(" where smsType = '").append(smsType).append("'");

        if(!isUpdataSMS){
            return AjaxResult.errorResult("您还没有更新任何信息");
        }else{
            try {
                UserDaoSql.getInstance().executeBySql(sb.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("更新失败，请稍后再试.");
            }
        }
        SysConfigSmsCache.getInstance().run();
        return AjaxResult.successResult();
    }
}